package cn.keniykey.mc.landterritory.config;

import java.io.File;

import org.bukkit.configuration.file.YamlConfiguration;

import cn.keniykey.mc.landterritory.LandTerritoryMain;
import cn.keniykey.mc.landterritory.toolkit.Util;

public final class LanguageManager {
	private static final String FILE_NAME = "language.yml";
	private static final int VERSION = 1;
	private static YamlConfiguration language_yaml;
	private static String str_prefix,str_chatprefix;
	
	private LanguageManager() {}
	
	private static YamlConfiguration getLanguageYamlForDir(boolean force) {
		final LandTerritoryMain main = LandTerritoryMain.getPlugin();
		final File dirfile = main.getDataFolder();
		dirfile.mkdirs();
		final File langfile = new File(dirfile, FILE_NAME);
		if(force || !langfile.exists()) {
			if(main.getResource(FILE_NAME) != null) {
				main.saveResource(FILE_NAME, true);
			}else {
				main.getLogger().warning(FILE_NAME + " null");
				return new YamlConfiguration();
			}
		}
		return YamlConfiguration.loadConfiguration(langfile);
	}
	public static void reloadLanguageConfiguration() {
		language_yaml = getLanguageYamlForDir(false);
		if(language_yaml.getInt("version") != VERSION) {
			language_yaml = getLanguageYamlForDir(true);
			LandTerritoryMain.getPlugin().getLogger().warning("配置文件 " + FILE_NAME + " 的version 不是 " + VERSION);
		}
		str_prefix = getLang(1000000);
		str_chatprefix = str_prefix + " ";
	}
	public static YamlConfiguration getLanguageYaml() {
		return language_yaml;
	}
	public static String getLang(int lang,boolean conver) {
		final String name = "lang-" + lang;
		final String str = language_yaml.getString(name, name);
		if(conver) return Util.conver(str);
		return str;
	}
	/**
	 * 自带转换conver
	 */
	public static String getLang(int lang) {
		return getLang(lang, true);
	}
	public static String getLang(int lang, Object...objs) {
		return Util.setStr(getLang(lang), objs);
	}
	public static String getChatLang(int lang) {
		return str_chatprefix + getLang(lang);
	}
	public static String getChatLang(int lang,Object...objs) {
		return str_chatprefix + Util.setStr(getLang(lang), objs);
	}
	public static String getChatPrefix() {
		return str_chatprefix;
	}
	public static String getPrefix() {
		return str_prefix;
	}
}